package com.archstoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.ChoiceAddressEntity;

import java.util.List;

/**
 * 选择所在地页面列表适配器
 */
public class ChoiceAddressAdapter extends BaseAdapter {

    private Context mContext;
    private List<ChoiceAddressEntity.DataBean> mData;

    private int mIndex;
    private String mKey;

    public ChoiceAddressAdapter(Context context, String key) {
        mContext = context;
        mKey = key;
    }

    public void setData(List<ChoiceAddressEntity.DataBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setIndex(int index) {
        mIndex = index;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public ChoiceAddressEntity.DataBean getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_choice_address, null);
            holder.itemTv = (TextView) view.findViewById(R.id.item_tv);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.itemTv.setText(mData.get(i).getRegion_name());
        holder.itemTv.setSelected(mIndex == i ? true : false);
//        holder.itemTv.setSelected(mKey.equalsIgnoreCase(mData.get(i).getRegion_name()));
        return view;
    }

    class Holder {
        TextView itemTv;
    }
}